<template>
  <div class="musicList">
    <div class="musicTop">
      <div class="title">发现好歌单</div>
      <div class="more">查看更多</div>
    </div>
    <div class="musicContent">
      <van-swipe
        :loop="false"
        :width="100"
        class="mySwipe"
        :show-indicators="false"
      >
        <van-swipe-item
          v-for="item in states.musicList"
          :key="item.id"
          style="margin-right: 10px"
          @click="detailMusic(item.id)"
        >
          <img :src="item.picUrl" alt="" srcset="" />
          <span class="playCount">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-24gl-play"></use>
            </svg>
            {{ playcount(item.playCount) }}
          </span>
          <span class="name">{{ item.name }}</span>
        </van-swipe-item>
      </van-swipe>
    </div>
  </div>
</template>

<script>
import { reqFundMusicListInfo } from "@/api/home";
import { reactive, onMounted } from "vue";
import { useRouter } from "vue-router";

export default {
  setup() {
    //数据
    let states = reactive({
      musicList: [],
    });
    //使用路由 进行跳转
    const $router = useRouter();

    //钩子
    onMounted(async () => {
      let results = await reqFundMusicListInfo();
      if (results.status == 200) {
        states.musicList = results.data.result;
      }
    });

    //方法  计算播放量
    function playcount(num) {
      let newnum = (num / 10000).toFixed(1) + "万";
      return newnum;
    }
    //方法  进行路由跳转
    function detailMusic(id) {
      $router.push({
        name: "ItemMusic",
        query: {
          id,
        },
      });
    }
    return {
      states,
      playcount,
      detailMusic,
    };
  },
};
</script>

<style lang="less" scoped>
.musicList {
  width: 100%;
  height: 5rem;
  padding: 0.2rem;
  .musicTop {
    width: 100%;
    height: 0.6rem;
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.2rem;
    .title {
      font-size: 0.4rem;
      font-weight: 900;
    }
    .more {
      border: 1px solid #ccc;
      text-align: center;
      line-height: 0.6rem;
      padding: 0 0.2rem;
      border-radius: 0.4rem;
    }
  }
}
.musicContent {
  width: 100%;
  height: 3rem;
  .mySwipe {
    height: 100%;
    font-size: 0.24rem;
    img {
      position: relative;
      width: 100%;
      height: 2rem;
      border-radius: 10%;
    }
    .playCount {
      position: absolute;
      top: 0.1rem;
      right: 0.12rem;
      color: white;
      opacity: 0.8;
      .icon {
        vertical-align: middle;
        width: 0.3rem;
      }
    }
  }
  .van-swipe-item:last-child {
    display: none;
  }
}
</style>